package models

import (
	"time"

	"github.com/astaxie/beego/orm"
)

func GetCveUserByUser(username, pwd string) (maps []orm.Params, err error) {
	o := orm.NewOrm()
	var num int64
	num, err = o.Raw("select user_id,expiration_time,aes_key FROM cve_other_user WHERE user_name=? and pass_word=?",
		username, pwd).Values(&maps)
	if err == nil && num > 0 {
		return maps, nil
	}
	return nil, err
}

func UpdateToken(Userid interface{}, token string, newTime time.Time) bool {
	o := orm.NewOrm()
	_ = o.Raw("update cve_other_user set aes_key=?,expiration_time=? where user_id=?",
		token, newTime, Userid).QueryRow()
	return true
}

func GetUserByToken(token string, ou *OtherUser) (err error) {
	o := orm.NewOrm()
	err = o.Raw("select * FROM cve_other_user WHERE aes_key=?", token).QueryRow(ou)
	return err
}
